<template>
	<view>
		<zy-cell-item label="账号安全" @click.native="toAccount"></zy-cell-item>
		<zy-cell-item label="内存清理" :content="cacheSize" @click.native="onClearCache"></zy-cell-item>
		<zy-cell-item label="用户协议" @click.native="onAgreement"></zy-cell-item>
		<zy-cell-item label="隐私政策" @click.native="onPrivacy"></zy-cell-item>
		<zy-cell-item label="个性化推送" :arrow="false">
			<switch slot="right-icon" :checked="pushChecked" @change="onPushChange"></switch>
		</zy-cell-item>
		<zy-cell-item label="版本号" :content="version" :arrow="false"></zy-cell-item>
		<zy-cell-item label="关于我们" @click.native="toAbout"></zy-cell-item>
		<!-- #ifdef APP-PLUS || H5 -->
		<zy-button text="退出登录" @click.native="onLogout" style="margin: 100rpx 30rpx;"></zy-button>
		<!-- #endif -->
	</view>
</template>

<script>
	import LoginUtil from '@/main/common/LoginUtil.js';
	import RouteUtil from '@/main/common/RouteUtil.js';
	export default {
		data() {
			return {
				cacheSize: '',
				pushChecked: false,
				version: '',
			}
		},
		onShow() {
			this.getCacheSize();
			this.getPushChecked();
			this.getVersion();
		},
		methods: {
			toAccount() {
				RouteUtil.toAccount()
			},
			onClearCache() {
				uni.showModal({
					title: "操作确认",
					content: "确认要清理缓存吗?",
					success: (res) => {
						if (res.confirm) {
							this.clearCache()
						}
					}
				})
			},
			onAgreement() {
				RouteUtil.toAgreement()
			},
			onPrivacy() {
				RouteUtil.toPrivacy()
			},
			onPushChange(event) {
				uni.setStorageSync('push', event.detail.value);
			},
			toAbout() {
				uni.navigateTo({
					url: '/main/about/about'
				})
			},
			async onLogout() {
				LoginUtil.logout()
			},
			//以下
			getCacheSize() {
				// #ifdef APP-PLUS
				let self = this;
				plus.cache.calculate(size => {
					if (size < 1024) {
						self.cacheSize = size + 'B';
					} else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
						self.cacheSize = Math.floor((size / 1024) * 100) / 100 + 'KB';
					} else if (size / 1024 / 1024 >= 1) {
						self.cacheSize = Math.floor((size / 1024 / 1024) * 100) / 100 + 'M';
					}
				});
				// #endif
			},
			clearCache() {
				let that = this;
				// #ifdef APP-PLUS
				let os = plus.os.name;
				if (os == 'Android') {
					let main = plus.android.runtimeMainActivity();
					let sdRoot = main.getCacheDir();
					let files = plus.android.invoke(sdRoot, "listFiles");
					let len = files.length;
					for (let i = 0; i < len; i++) {
						let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
						plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  	
									that.getAppInfo(); // 重新计算缓存
								}, function(e) {
									console.log(e.message)
								});
							} else {
								entry.remove();
							}
						}, function(e) {
							console.log('文件路径读取失败')
						});
					}
					uni.showToast({
						title: '缓存清理完成',
						duration: 2000
					});
					this.cacheSize = '0KB'
				} else { // ios  
					plus.cache.clear(function() {
						uni.showToast({
							title: '缓存清理完成',
							duration: 2000
						});
						this.cacheSize = '0KB'
					});

				}
				// #endif
			},
			getPushChecked() {
				let checked = uni.getStorageSync('push');
				if (checked) {
					this.pushChecked = checked;
				}
			},
			getVersion() {
				// #ifdef APP-PLUS
				plus.runtime.getProperty(plus.runtime.appid, (info) => {
					this.version = 'v' + info.version;
				});
				// #endif
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>